package weaver.formmode.customjavacode.customsearch;

import java.util.*;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;

/**
 * 绩效下发
 *
 * @author Administrator
 */
public class CustomSearchTemplate extends AbstractCustomSqlConditionJavaCode {

    /**
     * 生成SQL查询限制条件
     *
     * @param param param包含(但不限于)以下数据
     *              user 当前用户
     * @return 返回的查询限制条件的格式举例为: t1.a = '1' and t1.b = '3' and t1.c like '%22%'
     * 其中t1为表单主表表名的别名
     */

    public String generateSqlCondition(Map<String, Object> param) throws Exception {
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();

        User user = (User) param.get("user");
        int userid = user.getUID();
        baseBean.writeLog("generateSqlCondition——userid:"+userid);

        String sqlCondition = " 1=1 ";

        String tablename = baseBean.getPropValue("Mode_jxxfhz", "SJGLYtablename");

        boolean flag = false;
        rs.executeQuery("select ry from " + tablename);
        while (rs.next()) {
            if (user.getUID() == Util.getIntValue(rs.getString("ry"))) {
                flag = true;
                break;
            }
        }

        baseBean.writeLog("generateSqlCondition——flag:"+flag);

        if (user.getUID() != 1 && !flag) {
            int departmentid = user.getUserDepartment();
            rs.executeQuery("select departmentcode from hrmdepartment where id=?", departmentid);
            rs.next();
            String departmentcode = Util.null2String(rs.getString("departmentcode"));//部门编码
            sqlCondition = " t1.BMBM='" + departmentcode + "' ";

            baseBean.writeLog("generateSqlCondition——departmentid:"+departmentid+";departmentcode:"+departmentcode);
        }
        baseBean.writeLog("generateSqlCondition——sqlCondition:"+sqlCondition);

        return sqlCondition;
    }

}
